package com.mingke.chat.message.logic;

import com.mingke.chat.config.RedisKeyEnum;
import com.mingke.chat.message.EasyChatMessage;
import com.mingke.chat.message.EasyChatMessageType;
import com.mingke.chat.message.request.EasyChatGroupRequest;
import com.mingke.chat.message.response.CommonChatResponse;
import com.mingke.chat.message.response.EasyChatGroupResponse;
import com.mingke.chat.message.response.EasyChatResponse;
import com.mingke.chat.server.EasyChatClientChannelTool;
import com.mingke.common.entity.UserEntity;
import com.mingke.common.service.GroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 群聊消息的逻辑处理类
 */
public class EasyChatGroupRequestLogic implements EasyChatMessageLogic{
    //日志框架
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    //redis
    private RedisTemplate redisTemplate = null;
    //日期格式化工具
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //群组Service
    private GroupService groupService;
    //群友列表
    private List<UserEntity> mates;
    //响应
    EasyChatGroupResponse groupResponse = new EasyChatGroupResponse();

    @Override
    public void prepare(EasyChatMessage message) {
        EasyChatGroupRequest groupRequest = (EasyChatGroupRequest) message;
        //获取群友列表
        mates = groupService.mates(groupRequest.getGroupID());
        //准备响应内容
        BeanUtils.copyProperties(groupRequest, groupResponse);
        dispatch(message);
    }

    @Override
    public void doStock(EasyChatMessage message) {
        //对于不在线的群友，将消息存储到redis备用
        redisTemplate.opsForList().leftPush(groupResponse.getToNumber().toString(), groupResponse);
    }

    @Override
    public void dispatch(EasyChatMessage message) {
        /*
         * 1、查询到各位群友的channel
         * 2、在线的群友发送消息，不在线的群友暂存到redis
         */
        EasyChatGroupRequest groupRequest = (EasyChatGroupRequest) message;
        CommonChatResponse chatResponse = new CommonChatResponse(1, EasyChatMessageType.GroupResponse.getMessageCode(), new EasyChatResponse[]{groupResponse});
        for(UserEntity mate : mates) {
            if(mate.getUserLoginNumber().equals(groupRequest.getFromNumber())) {  //不用给消息的发送者推送消息
                continue;
            }
            //设置群组消息分发到的用户ID
            groupResponse.setToNumber(mate.getUserLoginNumber());
            if(redisTemplate.opsForHash().hasKey(RedisKeyEnum.UserONLine.getKeyName(), mate.getUserLoginNumber().toString())) {
                Boolean isOnLine = (Boolean) redisTemplate.opsForHash().get(RedisKeyEnum.UserONLine.getKeyName(), mate.getUserLoginNumber().toString());
                if(isOnLine) {
                    EasyChatClientChannelTool.instance.writeToClient(mate.getUserLoginNumber(), chatResponse);
                }else {
                    doStock(groupResponse);
                }
            }else {
                log.warn("群友的通讯信道尚未开启，投递至此群友的消息将会丢弃");
            }
        }
    }

    @Override
    public void doLogic(EasyChatMessage message) {
        prepare(message);
    }

    public EasyChatGroupRequestLogic() {
        this.redisTemplate = (RedisTemplate) EasyChatLogicContext.instance.getContext().getBean("redisTemplate");
        this.groupService = EasyChatLogicContext.instance.getContext().getBean(GroupService.class);
    }
}
